package com.shaoshaohuo.app.view.citymenu;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;
/**
 * @author LILIN
 * 下午1:48:27
 * 提供PopWindow调用方法
 */
public class CascadingMenuPopWindow extends PopupWindow{

	private CascadingMenuPopWindow cascadingMenuPopWindow;
	private Context context;
	private CascadingMenuView cascadingMenuView;
    //提供给外的接口
	private CascadingMenuViewOnSelectListener menuViewOnSelectListener;
	
	private boolean isWithCountry = true;
	
	public void setMenuViewOnSelectListener(
			CascadingMenuViewOnSelectListener menuViewOnSelectListener) {
		this.menuViewOnSelectListener = menuViewOnSelectListener;
	}
     
	public CascadingMenuPopWindow(Context context) {
		super(context);
		this.context=context;
		init();
	}
	public CascadingMenuPopWindow(Context context, boolean isWithCountry) {
		super(context);
		this.context=context;
		this.isWithCountry = isWithCountry;
		init();
	}
//
//	private ArrayList<Area> initProvince(Context context) {
//		CityDBhelper dBhelper = new CityDBhelper(context,isWithCountry);
//		return dBhelper.getProvince();
//	}
	
	public void init(){
		setOutsideTouchable(true);
		//实例化级联菜单
		cascadingMenuView=new CascadingMenuView(context, isWithCountry);
		setContentView(cascadingMenuView);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.MATCH_PARENT);
		//设置回调接口
		cascadingMenuView.setCascadingMenuViewOnSelectListener(new MCascadingMenuViewOnSelectListener());
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha)
	{
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
			lp.alpha = bgAlpha; //0.0-1.0
			activity.getWindow().setAttributes(lp);
		}
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		backgroundAlpha(0.3f);
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		backgroundAlpha(0.3f);
	}

	@Override
	public void dismiss() {
		super.dismiss();
		backgroundAlpha(1);
	}

	//级联菜单选择回调接口
	class MCascadingMenuViewOnSelectListener implements CascadingMenuViewOnSelectListener{

		@Override
		public void getValue(Area city, String fullname) {
			if(menuViewOnSelectListener!=null){
				menuViewOnSelectListener.getValue(city, fullname);
				dismiss();
			}
		}
		
	}
}
